// __PUBLIC__/js/page/admin/content/editad
require(['../../common'], function() {
  require(['content.editad'])
})

define('content.editad', [
  'jquery',
  'jqp/validate',
  'css!ke/themes/default/default.css',
  'ke/kindeditor'
], function($) {
  'use strict';

  $('#J_form').validate({
    classPlace: '.kv',
    errorPlace: '.kv-error',
    errors: {
      title: {
        required: '请填写广告名称',
      },
    }
  })

  $(document).ready(function() {
    var typeId = $('#A_typeId').val()
    bindListener()
    changeStyle(typeId)
  })
    .on('click', '#J_addimg', function(e) {
      e.preventDefault()
      addimg()
    })
    .on('click', '.A_delbanner', function(e) {
      e.preventDefault()
      delbanner(this)
    })
    .on('click', '.A_delImg', function(e) {
      e.preventDefault()
      delImg(this)
    })

  function bindListener() {
    $('a[name=rmlink]').unbind().click(function() {
      $(this).parent().remove()
    })
  }

  function delImg(obj) {
    var pObj = $(obj).parent()
    pObj.remove()
  }

  function delbanner(obj) {
    var ids = $(obj).attr('data-id')
    $.post(APP + '?app=admin&m=Content&a=deleteBanner', {
      id: ids
    }).done(function(res) {
      if (res == '1') {
        alert(lang.delSuccess)
        var pObj = $(obj).parent()
        pObj.remove()
      } else {
        alert(lang.delFails)
      }
    })
  }

  function addimg() {
    /*var banner = $('input[id=banner]').val();
      var url = $('input[id=bannerUrl]').val();*/
    $('#mdiv').append('<div> <span style="color:red">*</span>Banner图片：<input type="file" name="banner[]" value=""  id="banner"/><span style="color:red">*</span> 链接地址：<input type="text" class="input-text" name="bannerUrl[]" value="" id="bannerUrl"><a href="javascript:void(0);" class="A_delImg" name="rmlink"> 删除</a> <br></div>')
    bindListener()
  }

  function changeStyle(value) {
    if (value === '' || value === '1') {
      $('#J_content1').hide()
      $('#J_content3').hide()
      $('#J_content').show()
    }
    if (value === '2') {
      $('#J_content').hide()
      $('#J_content3').hide()
      $('#J_content1').show()
    }
    if (value === '3') {
      $('#J_content').hide()
      $('#J_content1').hide()
      $('#J_content3').show()
    }
  }
  
  var $editor = $('#J_content')
  if ($editor.length) {
    KindEditor.create($editor[0],{langType : 'zh_CN',})
  }
  
})
